home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Graphics / Utility / GL Viewer 1.1.1 / src ƒ / memoryio.c < prev    next >
Text File  |  1993-08-28  |  4KB  |  201 lines

  1. /*
  2.  * Routines to perform stdio-like in-memory file operations.
  3.  *
  4.  * Copyright (c) 1993 by Martin W. Fong
  5.  */
  6.  
  7.  
  8. #include "memoryio.h"
  9.  
  10.  
  11.     MemoryFile *
  12. mopen (const char *filename, const char *mode)
  13.  
  14. {
  15.     MemoryFile *retVal = (MemoryFile *) malloc ((size_t) sizeof (MemoryFile));
  16.  
  17.  
  18.     if (retVal /* != (MemoryFile *) NULL */)
  19.     {
  20.         retVal->filename = filename;
  21.         retVal->buffer     = (char *) NULL;
  22.         retVal->bufLen     = 0L;
  23.         retVal->currPos  = 0L;
  24.     }
  25.  
  26.     return retVal;
  27. }
  28.  
  29.  
  30.     int
  31. mclose (MemoryFile *stream)
  32.  
  33. {
  34.     if (stream /* != (MemoryFile *) NULL */)
  35.     {
  36.         if (stream->buffer /* != (char *) NULL */)
  37.             free (stream->buffer);
  38.  
  39.         free ((char *) stream);
  40.     }
  41.  
  42.     return EOF;
  43. }
  44.  
  45.  
  46.     int
  47. mseek (MemoryFile *stream, long offset, int whence)
  48.  
  49. {
  50.     if (stream /* != (MemoryFile *) NULL */)
  51.     {
  52.         long    newCurrPos = -1L;
  53.  
  54.  
  55.         switch (whence)
  56.         {
  57.             case SEEK_SET:
  58.             {
  59.                 newCurrPos = offset;
  60.                 break;
  61.             }
  62.  
  63.             case SEEK_CUR:
  64.             {
  65.                 newCurrPos = stream->currPos + offset;
  66.                 break;
  67.             }
  68.  
  69.             case SEEK_END:
  70.             {
  71.                 newCurrPos = stream->bufLen - offset;
  72.                 break;
  73.             }
  74.         }
  75.  
  76.         if (stream->buffer == (char *) NULL &&
  77.             0L < newCurrPos)
  78.         {
  79.              stream->buffer = (char *) malloc (newCurrPos + 1L);
  80.     
  81.              if (stream->buffer /* != (char *) NULL */)
  82.              {
  83.                  stream->bufLen  = newCurrPos + 1;
  84.                  stream->currPos = 0L;
  85.              }
  86.         }
  87.  
  88.         if (0L <= newCurrPos && newCurrPos < stream->bufLen)
  89.         {
  90.             stream->currPos = newCurrPos;
  91.             return 0;
  92.         }
  93.     }
  94.  
  95.     return -1;
  96. }
  97.  
  98.  
  99.     long
  100. mtell (MemoryFile *stream)
  101.  
  102. {
  103.     if (stream /* != (MemoryFile *) NULL */)
  104.         return stream->currPos;
  105.  
  106.     return -1L;
  107. }
  108.  
  109.  
  110.     size_t
  111. mread (void *array, size_t size, size_t numitems, MemoryFile *stream)
  112.  
  113. {
  114.     if (stream /* != (MemoryFile *) NULL */ &&
  115.         stream->currPos < stream->bufLen &&
  116.         size /* > 0 */ && numitems /* > 0 */ &&
  117.         array /* != (void *) NULL */)
  118.     {
  119.         unsigned long    bytesLeft = stream->bufLen - stream->currPos;
  120.         size_t        maxNumItems = bytesLeft / size;
  121.         unsigned long    bytesRead;
  122.  
  123.  
  124.         if (maxNumItems < numitems)
  125.             numitems = maxNumItems;
  126.  
  127.         if ((bytesRead = size * numitems) /* > 0 */)
  128.         {
  129.             (void) memcpy (array, stream->buffer + stream->currPos, bytesRead);
  130.  
  131.             stream->currPos += bytesRead;
  132.         }
  133.  
  134.         return numitems;
  135.     }
  136.  
  137.     return (size_t) 0;
  138. }
  139.  
  140.  
  141.     size_t
  142. mwrite (const void *array, size_t size, size_t numitems, MemoryFile *stream)
  143.  
  144. {
  145.     if (stream /* != (MemoryFile *) NULL */ &&
  146.         stream->currPos < stream->bufLen &&
  147.         size /* > 0 */ && numitems /* > 0 */ &&
  148.         array /* != (void *) NULL */)
  149.     {
  150.         unsigned long    bytesLeft = stream->bufLen - stream->currPos;
  151.         size_t            maxNumItems = bytesLeft / size;
  152.         unsigned long    bytesWrote;
  153.  
  154.  
  155.         if (maxNumItems < numitems)
  156.             numitems = maxNumItems;
  157.  
  158.         if ((bytesWrote = size * numitems) /* > 0 */)
  159.         {
  160.             (void) memcpy (stream->buffer + stream->currPos, array, bytesWrote);
  161.  
  162.             stream->currPos += bytesWrote;
  163.         }
  164.  
  165.         return numitems;
  166.     }
  167.  
  168.     return (size_t) 0;
  169. }
  170.  
  171.  
  172.     int
  173. mgetc (MemoryFile *stream)
  174.  
  175. {
  176.     if (stream /* != (MemoryFile *) NULL */ &&
  177.         stream->currPos < stream->bufLen)
  178.     {
  179.         unsigned char uc = (unsigned char ) stream->buffer[stream->currPos++];
  180.  
  181.  
  182.         return (uc);
  183.     }
  184.  
  185.     return EOF;
  186. }
  187.  
  188.  
  189.     int
  190. mputc (char c, MemoryFile *stream)
  191.  
  192. {
  193.     if (stream /* != (MemoryFile *) NULL */ &&
  194.         stream->currPos < stream->bufLen)
  195.     {
  196.         return (stream->buffer[stream->currPos++] = c);
  197.     }
  198.  
  199.     return EOF;
  200. }
  201.